﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MDIApplication.Classes;

namespace MDIApplication
{
    public partial class GuestManagement : Form
    {
        public GuestManagement()
        {
            InitializeComponent();
        }

        #region Form_Load
        private void GuestManagement_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'guestManagementDataSet.Guest' table. You can move, or remove it, as needed.
            this.guestTableAdapter.Fill(this.guestManagementDataSet.Guest);
            
            dataGridView1.AllowUserToResizeRows = false;
            dataGridView1.ReadOnly = true;
        }
        #endregion Form_Load

        #region  dataGridView

        DataView guestDv = new DataView();
        private void Reload_dataGridView()
        {
            DataTable guestTab = new DataTable();
            SqlConnection connection = new SqlConnection(@"Data Source=LUCKYPX\LUCKYPX;Initial Catalog=SWENAssignment3;Persist Security Info=True;User ID=sa;Password=imsa");
            connection.Open();
            guestTab.Load(new SqlCommand("Select * From Guest", connection).ExecuteReader());
            dataGridView1.DataSource = guestDv = guestTab.DefaultView;
            connection.Close();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (e.ColumnIndex == dataGridView1.Columns["Edit"].Index)
                {
                    dataGridView1.ReadOnly = false;
                   dataGridView1.BeginEdit(true);
                }
                else if (e.ColumnIndex == dataGridView1.Columns["Delete"].Index)
                {
                    DialogResult dialogResult = MessageBox.Show("Are you sure delete this guest record?", "Delete Guest", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                    if (dialogResult == DialogResult.Yes)
                    {
                        DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
                        BookingRoomAvailabilityDBManager db = new BookingRoomAvailabilityDBManager();
                        string guestID = row.Cells[0].Value.ToString();
                        db.DeleteGuestByGuestId(guestID);
                        Reload_dataGridView();
                    }                  
                }                
            }
        }


        #endregion dataGridView

        #region toolStrip

        NewBooking newBookingChild;
        AllBookingscs allBookingChild;
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (allBookingChild == null)
            {
                allBookingChild = new AllBookingscs();
                allBookingChild.Location = new Point(0, 0);
                // Set the Parent Form of the Child window.
                allBookingChild.MdiParent = this.MdiParent;
                // Display the form.
                allBookingChild.Show();
                allBookingChild.Activate();
                if (newBookingChild == null)
                {
                    newBookingChild = new NewBooking();
                    newBookingChild.MdiParent = this.MdiParent;
                    newBookingChild.Location = new Point(100, 70);
                    // Display the form.
                    newBookingChild.Show();
                    newBookingChild.Activate();
                }
            }
        }

        CheckIn checkInChild;
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (checkInChild == null)
            {
                checkInChild = new CheckIn();
                checkInChild.MdiParent = this.MdiParent;
                checkInChild.Location = new Point(0, 0);
                checkInChild.Show();
                checkInChild.Activate();
            }
            this.Close();
        }

        CheckOut checkOutChild;
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (checkOutChild == null)
            {
                checkOutChild = new CheckOut();
                checkOutChild.MdiParent = this.MdiParent;
                checkOutChild.Location = new Point(0, 0);
                checkOutChild.Show();
                checkOutChild.Activate();
            }
            this.Close();
        }

        GuestManagement guestManagementChild;
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (guestManagementChild == null)
            {
                guestManagementChild = new GuestManagement();
                guestManagementChild.Location = new Point(0, 0);
                guestManagementChild.MdiParent = this.MdiParent;
                guestManagementChild.Show();
                guestManagementChild.Activate();
            }
        }
        #endregion toolStrip

        #region CloseAllForms
        private void CloseAllForms()
        {
            Form[] formToClose = null;
            int i = 1;
            foreach (Form form in Application.OpenForms)
            {
                if (form != this && form.IsMdiChild) //this is form1
                {
                    Array.Resize(ref formToClose, i);
                    formToClose[i - 1] = form;
                    i++;
                }
            }
            if (formToClose != null)
            {
                for (int j = 0; j < formToClose.Length; j++)
                {
                    formToClose[j].Close();
                }
                checkInChild = null;
                checkOutChild = null;
                newBookingChild = null;
                allBookingChild = null;
                guestManagementChild = null;
            }
        }
        #endregion CloseAllForms

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                Guest ng = new Guest();
                ng.guestId = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
                ng.firstName = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[1].Value);
                ng.lastName = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[2].Value);
                ng.icNo = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[3].Value);
                ng.dateOfBirth = Convert.ToDateTime(dataGridView1.Rows[e.RowIndex].Cells[4].Value);
                ng.gender = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[5].Value);
                ng.address = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[6].Value);
                ng.contactNo = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[7].Value);
                ng.email = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[8].Value);

                BookingRoomAvailabilityDBManager db = new BookingRoomAvailabilityDBManager();
                db.updateGuestInfo(ng);
            }
        }


    }
}
